package com.cbd.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.cbd.dao.RecordDao;
import com.cbd.pojo.PageBean;
import com.cbd.pojo.Record;
@Component
public class RecordDaoImpl implements RecordDao{
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	@Override
	public void addRecord(Record record) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.save(record);
	}

	@Override
	public PageBean findAllRecord(int ladyId,PageBean page) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Record r where r.purpose.rentInfo.personalcarport.landlady.ladyID = ?");
		int num = (page.getCurPage()-1)*page.getPageSize();
		query.setInteger(0, ladyId);
		query.setFirstResult(num);
		query.setMaxResults(page.getPageSize());
		List<Record> list = query.list();
		page.setList(list);
		return page;
	}

	@Override
	public List<Record> findAllRecordByRenterId(int renterId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int findRecordNum(int ladyID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("select count (*) from Record r where r.purpose.rentInfo.personalcarport.landlady.ladyID = ?");
		query.setInteger(0, ladyID);
		Long l = (Long) query.uniqueResult();
		return l.intValue();
	}

	@Override
	public Record findRecordById(int recordID) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Record r = (Record) session.load(Record.class, recordID);
		return r;
	}

	@Override
	public List<Record> findRecord() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Record r");
		List<Record> list = query.list();
		return list;
	}

}
